package com.cy.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cy.util.CookieUtil;

/**
 * Servlet implementation class LogoutServlet
 */
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		// 清除保存在session中的用户信息
		request.getSession().removeAttribute("judge");
		request.getSession().removeAttribute("session_user");
		request.getSession().removeAttribute("nowuser");
		request.getSession().removeAttribute("gengraluser");
		request.getSession().removeAttribute("adminuser");

		// 同时也清除保存在cookie中的用户信息
		Cookie cookie = CookieUtil.getCookieByName(request, "cookie_name_pass_judge");
		if (cookie != null) {
			cookie.setMaxAge(0);
			cookie.setPath(request.getContextPath());
			// 将cookie响应出去
			response.addCookie(cookie);
		}
		response.sendRedirect("index.jsp");
	}

}
